﻿using MediatR;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.Organization;
using S.Authorization.Infrastructure.Interface.Organization;
using S.Authorization.Read.API.Application.Command.OrganizationCommand;
using System.Runtime.CompilerServices;

namespace S.Authorization.Read.API.Application.CommandHandlers.OrganizationCommandHandler
{
    public class IndustryModelCommandHandler : IRequestHandler<IndustryModelCommand, List<DtoCasCade>>
    {
        private readonly IIndustryModelRepository industry;
        public IndustryModelCommandHandler(IIndustryModelRepository _industry)
        {
            this.industry = _industry;
        }
        /// <summary>
        /// 级联
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        private List<DtoCasCade> GetIndustry(int menuId)
        {
            List<IndustryModel> childList =  industry.GetAllAsync().Result.Where(x => x.ParentIndustry == menuId).ToList();
            List<DtoCasCade> children = childList.Select(x => new DtoCasCade
            {
                value = x.IndustryId,
                label = x.IndustryName,
               children = GetIndustry(x.IndustryId).Count() == 0 ? null : GetIndustry(x.IndustryId),
            }).ToList();
            return children;
        }
        /// <summary>
        /// 显示行业列表（周文浩）
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<DtoCasCade>> Handle(IndustryModelCommand request, CancellationToken cancellationToken)
        {
            return  GetIndustry(request.PID);
        }

       
        
       
    }
}
